package com.shisfish.library.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import com.shisfish.library.Impl.ItemImpl;
import com.shisfish.library.Model.Item;
import com.shisfish.library.Model.MediaType;
import com.shisfish.library.Prere.Login;
import com.shisfish.library.Utils.Global.GlobalVariable;
import com.shisfish.library.Utils.Global.toJsonUtils;

@Path("item")	
public class ItemController extends GlobalVariable {

	private ItemImpl itemImpl = new ItemImpl();
	
	/**
	 * @Author shisfish
	 * @Date 2016年1月4日 下午3:55:12
	 * @Descrition : 通过父类查找信息
	 * @param fcode
	 * @return
	 */
	@GET
	@Path("selectByFcode/{fcode}")
	@Produces("application/json; charset=utf-8")
	public String selectByFcode(@PathParam("fcode") int fcode) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		List<Item> items = itemImpl.selectByFcode(fcode);
		
		if (items.size() > 0) {
			globalMap.put("list", items);
			globalMap.put(STATUS_CODE, SUCCESS);
		} else if (items.size() == 0) {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		} else {
			globalMap.put(STATUS_CODE, SELECT_WR);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	/**
	 * @Author shisfish
	 * @Date 2016年1月4日 下午3:59:46
	 * @Descrition : 通过父类和本类查找信息
	 * @param fcode
	 * @param code
	 * @return
	 */
	@GET
	@Path("selectByCode/{fcode}/{code}")
	@Produces("application/json; charset=utf-8")
	public String selectByCode(@PathParam("fcode") int fcode, @PathParam("code") int code) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		List<Item> items = itemImpl.selectByCode(fcode, code);
		
		if (items.size() > 0) {
			globalMap.put("list", items.get(0));
			globalMap.put(STATUS_CODE, SUCCESS);
		} else if (items.size() == 0) {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		} else {
			globalMap.put(STATUS_CODE, SELECT_WR);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@POST
	@Path("insertAItem/{idadmin}/{fcode}")
	@Produces("application/json; charset=utf-8")
	public String insertAItem(@PathParam("idadmin") String idadmin, @PathParam("fcode") int fcode, 
			@Context HttpServletRequest request, 
			@FormParam("name") String name) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		if (Login.isLoginAdmin(idadmin, request)) {
			List<Item> items = itemImpl.selectByFcode(fcode);
			int count = items.size() + 1;
			Item item = new Item(count, name, fcode);
			int flag = itemImpl.insertOne(item);
			if (flag == 0) {
				globalMap.put(STATUS_CODE, INSERT_WR);
			} else {
				globalMap.put(STATUS_CODE, SUCCESS);
			}
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@POST
	@Path("updateAItem/{idadmin}/{fcode}/{code}")
	@Produces("application/json; charset=utf-8")
	public String updateAItem(@PathParam("idadmin") String idadmin, @Context HttpServletRequest request, 
			@FormParam("name") String name, @PathParam("fcode") int fcode, 
			@PathParam("code") int code) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		if (Login.isLoginAdmin(idadmin, request)) {
			
			List<Item> items = itemImpl.selectByCode(fcode, code);
			
			if (items.size() > 0) {
				Item item = items.get(0);
				item.setName(name);
				int flag = itemImpl.updateOne(item);
				if (flag == 0) {
					globalMap.put(STATUS_CODE, UPDATE_WR);
				} else {
					globalMap.put(STATUS_CODE, SUCCESS);
				}
			} else {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			}
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("deleteAItem/{idadmin}/{id}")
	@Produces("application/json; charset=utf-8")
	public String deleteAItem(@PathParam("idadmin") String idadmin, @Context HttpServletRequest request, 
			@PathParam("id") int id) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		if (Login.isLoginAdmin(idadmin, request)) {
			int flag = itemImpl.deleteOneById(id);
			if (flag == 0) {
				globalMap.put(STATUS_CODE, UPDATE_WR);
			} else {
				globalMap.put(STATUS_CODE, SUCCESS);
			}
		
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	
}
